package cn.tsmchina.one.service.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.tsmchina.one.dao.activity.ActivityDao;
import cn.tsmchina.one.dao.user.UserDao;
import cn.tsmchina.one.domain.activity.Activity;
import cn.tsmchina.one.domain.user.User;
import cn.tsmchina.one.pojo.ActivityPojo;
import cn.tsmchina.one.util.C;

@Service
public class ActivityServiceImpl implements ActivityService
{
	@Autowired
	private ActivityDao activityDao;
	
	@Autowired
	private UserDao userDao;
	
	@Override
	public Integer addActivity(Activity activity) {
		
		return activityDao.addActivity(activity);
	}
	
	@Override
	public List<ActivityPojo> getActivities(int page, long cid)
	{
		 List<Activity> list = activityDao.activities(page, cid);
		 List<ActivityPojo> activityPojos = new ArrayList<>();
		 for (Activity activity : list)
			activityPojos.add(new ActivityPojo(activity));
		 return activityPojos;
	}
	
	@Override
	public void deleteActivityById(int id) {
		activityDao.deleteById(id);
		
	}

	
	
	@Override
	public Activity getActivity(int id)
	{
		return activityDao.findActivity(id);
	}

	@Override
	public int getTotal(long cid)
	{
		return activityDao.total(cid);
	}

	@Override
	public List<Activity> getActivityNotice()
	{
		return activityDao.getActivityNotice();
	}

	@Override
	public void updateActivity(Activity activity) {
		activityDao.updateActivity(activity);
	}

	@Override
	public Map<String,Object> viewJoinsById(int id,int page,long customerId) {
		Map<String,Object> outMap = new HashMap<String, Object>();
		Activity activity = this.getActivity(id);
		if(null != activity){
			String uids = activity.getJoinUserId();
			List<User> userList =  userDao.getUserListByIds(uids,page,customerId);
			if(null != userList && userList.size()>0){
				int total = userDao.getUserTotalByIds(uids,customerId);
				int pageTotal = total % C.Paging.SIZE == 0 ? total / C.Paging.SIZE : total / C.Paging.SIZE + 1;
				
				outMap.put("users", userList);
				outMap.put("activity", activity);
				outMap.put("total", total);
				outMap.put("pageTotal", pageTotal);
				outMap.put("len", userList.size());
			}else{
				outMap.put("len", 0);
				outMap.put("total", 0);
				outMap.put("pageTotal", 0);
			}
			
			return outMap;
		}
		return null;
	}

}
